package com.jack.helloworld.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.jack.helloworld.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AbsListView.OnScrollListener {

    ListView simpleListView;
    SimpleAdapter simpleAdapter;

    List<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();


    String[] keys = new String[]{"pic", "text"};
    int[] ids = new int[]{R.id.item_img, R.id.item_text};

    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_list);

        initView();
        initDate();
    }


    private void initView() {


        simpleListView = (ListView) findViewById(R.id.simple_list);

    }

    private void initDate() {
        simpleAdapter = new SimpleAdapter(this, getDatas(), R.layout.list_item, keys, ids);
        simpleListView.setAdapter(simpleAdapter);

        simpleListView.setOnItemClickListener(this);
        simpleListView.setOnScrollListener(this);
    }

    private List<Map<String, Object>> getDatas() {

        for (int i = 0; i < 20; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("pic", R.mipmap.ic_launcher);
            map.put("text", "Android" + i);
            arrayList.add(map);
        }

        return arrayList;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String text = simpleListView.getItemAtPosition(position).toString();
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case SCROLL_STATE_FLING:
                Log.e("jack", "SCROLL_STATE_FLING");
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("pic", R.mipmap.ic_launcher);
                map.put("text", "new item " + index);
                index ++;
                arrayList.add(map);
                simpleAdapter.notifyDataSetChanged();

                break;
            case SCROLL_STATE_IDLE:
                Log.e("jack", "SCROLL_STATE_IDLE");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.e("jack", "SCROLL_STATE_TOUCH_SCROLL");
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Log.d("jack", "onScroll");

    }

}
